/*
 * File:   myType.h
 * Author: aple
 * version : v1.0.0
 * Created on 2014年7月5日, 下午5:51
 */

#ifndef MYTYPE_H
#define MYTYPE_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "stdint.h"

#ifndef bbool
#define bbool uint8_t
#endif

#ifdef UINT_GEN
	typedef unsigned char uint8, UINT8;
	typedef unsigned short uint16, UINT16;
	typedef unsigned int uint32, UINT32;
	typedef unsigned long long uint64, UINT64;
	typedef signed char int8, INT8;
	typedef signed short int16, INT16;
	typedef signed int int32, INT32;
	typedef signed long long int64, INT64;
#endif

#ifdef BYTE_GEN
	typedef unsigned char byte, BYTE;
	typedef unsigned short word, WORD;
	typedef unsigned int dword, DWORD;
	typedef unsigned long long qword, QWORD;
	typedef signed char ibyte, IBYTE;
	typedef signed short iword, IWORD;
	typedef signed int idword, IDWORD;
	typedef signed long long iqword, IQWORD;
#endif

#ifdef UCHAR_GEN
	typedef unsigned char uchar, UCHAR;
	typedef unsigned short ushort, USHORT;
	typedef unsigned int ulong, ULONG;
	typedef unsigned long long ullong, ULLONG;

	typedef signed char ichar, ICHAR;
	typedef signed short ishort, ISHORT;
	typedef signed int ilong, ILONG;
	typedef signed long long illong, ILLONG;
#endif

#ifdef U8_GEN
	typedef unsigned char u8, U8;
	typedef unsigned short u16, U16;
	typedef unsigned int u32, U32;
	typedef unsigned long long u64, U64;

	typedef signed char i8, I8;
	typedef signed short i16, I16;
	typedef signed int i32, I32;
	typedef signed long long i64, I64;

	typedef volatile unsigned char uv8, vu8, UV8, VU8;
	typedef volatile unsigned short uv16, vu16, UV16, VU16;
	typedef volatile unsigned int uv32, vu32, UV32, VU32;
	typedef volatile unsigned long long uv64, vu64, UV64, VU64;

	typedef volatile signed char iv8, vi8, IV8, VI8;
	typedef volatile signed short iv16, vi16, IV16, VI16;
	typedef volatile signed int iv32, vi32, IV32, VI32;
	typedef volatile signed long long iv64, vi64, IV64, VI64;

	typedef const unsigned char uc8, cu8, UC8, CU8;
	typedef const unsigned short uc16, cu16, UC16, CU16;
	typedef const unsigned int uc32, cu32, UC32, CU32;
	typedef const unsigned long long uc64, cu64, UC64, CU64;

	typedef const signed char ic8, ci8, IC8, CI8;
	typedef const signed short ic16, ci16, IC16, CI16;
	typedef const signed int ic32, ci32, IC32, CI32;
	typedef const signed long long ic64, ci64, IC64, CI64;
#endif

	typedef unsigned char uu8, UU8;
	typedef unsigned short uu16, UU16;
	typedef unsigned int uu32, UU32;
	typedef unsigned long long uu64, UU64;

	typedef signed char ii8, II8;
	typedef signed short ii16, II16;
	typedef signed int ii32, II32;
	typedef signed long long ii64, II64;

	typedef volatile unsigned char uuv8, vuu8, UUV8, VUU8;
	typedef volatile unsigned short uuv16, vuu16, UUV16, VUU16;
	typedef volatile unsigned int uuv32, vuu32, UUV32, VUU32;
	typedef volatile unsigned long long uuv64, vuu64, UUV64, VUU64;

	typedef volatile signed char iiv8, vii8, IIV8, VII8;
	typedef volatile signed short iiv16, vii16, IIV16, VII16;
	typedef volatile signed int iiv32, vii32, IIV32, VII32;
	typedef volatile signed long long iiv64, vii64, IIV64, VII64;

	typedef const unsigned char uuc8, cuu8, UUC8, CUU8;
	typedef const unsigned short uuc16, cuu16, UUC16, CUU16;
	typedef const unsigned int uuc32, cuu32, UUC32, CUU32;
	typedef const unsigned long long uuc64, cuu64, UUC64, CUU64;

	typedef const signed char iic8, cii8, IIC8, CII8;
	typedef const signed short iic16, cii16, IIC16, CII16;
	typedef const signed int iic32, cii32, IIC32, CII32;
	typedef const signed long long iic64, cii64, IIC64, CII64;

// 函数指针强制类型转换-模板
#define vfun(fun) ((void (*)(void))(fun))
#define vfun_vp(fun) ((void (*)(void *))(fun))
#define ifun(fun) ((int (*)(void))(fun))
#define ifun_vp(fun) ((int (*)(void *))(fun))
#define vfun_i32(fun) ((void (*)(i32))(fun))
#define vfun_i16(fun) ((void (*)(i16))(fun))
#define vfun_i8(fun) ((void (*)(i8))(fun))

// 定义函数指针-模板
#define def_vfun(fun) void (*fun)(void)
#define def_vfun_vp(fun) void (*fun)(void *)
#define def_ifun(fun) int (*fun)(void)
#define def_ifun_vp(fun) int (*fun)(void *)
#define def_vfun_i32(fun) void (*fun)(i32)
#define def_vfun_i32(fun) void (*fun)(i32)

// 一个数组/平面 指针 - 模:: type为元素类型
#define convert_arr_ptr(ptr, type, len) (type(*)[len])(ptr)
#define convert_plane_ptr(ptr, type, m, n) (type(*)[m][n])(ptr)
#define def_arr_ptr(ptr, type, len) type(*ptr)[len]
#define def_plane_ptr(ptr, type, m, n) type(*ptr)[m][n]
// 一组平面指针，每个元素指向一组平面
#define def_nplane_ptr(ptr, x, type, m, n) type(*ptr[x])[m][n]

// value
#ifndef ext_inrom
#define ext_inrom const
#endif

#ifndef ext_code
#define ext_code const
#endif

#ifndef EXT_TRUE
#define EXT_TRUE 1
#define EXT_FALSE 0
#endif

#ifndef EXT_ENABLE
#define EXT_ENABLE 1
#define EXT_DISABLE 0
#endif

#define DIS 0
#define ENA 1
#define PRG 2

#define LOG_FLAG_START 0x55
#define LOG_FLAG_DAT 0x33
#define LOG_FLAG_TAIL 0xCC

// get memory data
#ifndef outpw
#define outpw(port, value) *((volatile unsigned long *)(port)) = value
#define inpw(port) (*((volatile unsigned long *)(port)))
#define outps(port, value) *((volatile unsigned short *)(port)) = value
#define inps(port) (*((volatile unsigned short *)(port)))
#define outpb(port, value) *((volatile unsigned char *)(port)) = value
#define inpb(port) (*((volatile unsigned char *)(port)))
#define outp32(port, value) *((volatile unsigned long *)(port)) = value
#define inp32(port) (*((volatile unsigned long *)(port)))
#define outp16(port, value) *((volatile unsigned short *)(port)) = value
#define inp16(port) (*((volatile unsigned short *)(port)))
#define outp8(port, value) *((volatile unsigned char *)(port)) = value
#define inp8(port) (*((volatile unsigned char *)(port)))
#endif

// 指定地址读写浮点数
#define inpf32(port) (*((volatile float *)(port)))
#define outpf32(port, value) *((volatile float *)(port)) = value
#define inpf64(port) (*((volatile double *)(port)))
#define outfd64(port, value) *((volatile double *)(port)) = value
#define inp64(port) (*((volatile unsigned long long *)(port)))
#define outp64(port, value) *((volatile unsigned long long *)(port)) = value

#define ext_mem64(port) (*((volatile unsigned long long *)(port)))
#define ext_mem32(port) (*((volatile unsigned long *)(port)))
#define ext_mem16(port) (*((volatile unsigned short *)(port)))
#define ext_mem8(port) (*((volatile unsigned char *)(port)))

#ifndef mem32
#define mem64 ext_mem64
#define mem32 ext_mem32
#define mem16 ext_mem16
#define mem8 ext_mem8
#endif

//类型合并分离
#define byte_lb(x) (x & 0xFF)
#define byte_hb(x) ((x >> 8) & 0xFF)
#define byte_ub(x) ((x >> 16) & 0xFF)
#define byte_mb(x) ((x >> 24) & 0xFF)

#define word_lw(x) (x & 0xFFFF)
#define word_hw(x) ((x >> 16) & 0xFFFF)

#define word_make(hb, lb) ((uint16_t)((uint8_t)(lb) | ((uint8_t)(hb) << 8)))
#define dword_make(hw, lw) ((uint32_t)((uint16_t)(lw) | ((uint16_t)(hw) << 16)))
#define dword_wmake(hw, lw) ((uint32_t)((uint16_t)(lw) | ((uint16_t)(hw) << 16)))
#define dword_bmake(mb, ub, hb, lb) ((uint32_t)(((uint8_t)(mb) << 24) | ((uint8_t)(ub) << 16) | ((uint8_t)(hb) << 8) | ((uint8_t)(lb) << 0)))

// 地址a是否边界对齐于x
#define is_align(a, x) (0 == ((uint32_t)(a)) & ((x)-1))

//比较
#if !defined(ext_max)
#define ext_max(a, b) (((a) > (b)) ? (a) : (b))
#define ext_min(a, b) (((a) < (b)) ? (a) : (b))
#endif

#if !defined(MAX)
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif

#define ext_abs(x) (((x) < 0) ? (-(x)) : (x))

//交换
#define swap32(ap, bp)      \
	{                       \
		uint32_t temp = ap; \
		ap = bp;            \
		bp = temp;          \
	}

#define swap16(ap, bp)      \
	{                       \
		uint16_t temp = ap; \
		ap = bp;            \
		bp = temp;          \
	}

#define swap08(ap, bp)     \
	{                      \
		uint8_t temp = ap; \
		ap = bp;           \
		bp = temp;         \
	}

//-位处理-
#ifndef SetBit
#define SetBit(VAR, Pos) ((VAR) |= (1ul << (Pos)))
#define ClrBit(VAR, Pos) ((VAR) &= ~(1ul << (Pos)))
#define TogBit(VAR, Pos) ((VAR) ^= (1ul << (Pos)))
#define GetBit(VAR, Pos) (((VAR) >> (Pos)) & 1ul)
#endif

#ifndef set_bit
#define set_bit SetBit
#define clr_bit ClrBit
#define tog_bit TogBit
#define get_bit GetBit
#endif

#ifndef BIT_MASK
#define BIT_MASK(x) (1UL << (x))
#endif

#ifndef BIT
#define BIT BIT_MASK
#endif

#ifndef BITS_MASK
#define BITS_MASK(m, n) (((m) > (n)) ? ((UINT32_MAX >> (31 - (m))) & (UINT32_MAX << (n))) : ((UINT32_MAX >> (31 - (n))) & (UINT32_MAX << (m))))
#endif

#ifndef BITS
#define BITS BITS_MASK
#endif

#ifndef set_bits
#define set_bits(val, m, n) (val |= BITS_MASK(m, n))
#define clr_bits(val, m, n) (val &= ~BITS_MASK(m, n))
#define tog_bits(val, m, n) (val ^= BITS_MASK(m, n))
#define get_bits(val, m, n) (((m) > (n)) ? (((val)&BITS_MASK(m, n)) >> (n)) : (((val)&BITS_MASK(m, n)) >> (m)))
#endif

// -范围检测-
#define rang_offset(val, set, offset) ((((val) + (offset)) >= (set)) && ((val) <= ((set) + (offset))))
#define rang_check(val, min, max) (((val) >= (min)) && ((val) <= (max)))
#define rang_limit(val, min, max) \
	{                             \
		if (val < (min))          \
			val = min;            \
		if (val > (max))          \
			val = max;            \
	}
#define rang_limit_low(val, min) \
	{                            \
		if (val < (min))         \
			val = min;           \
	}

#define rang_limit_high(val, max) \
	{                             \
		if (val > (max))          \
			val = max;            \
	}

// array length get
#define array_len(x) (sizeof(x) / sizeof(x[0]))
#define arr_len array_len
#define arr_size array_len
#define arrray_size array_len

// ascii check
#define asc_dec_check(ch) (((ch) >= '0') && ((ch) <= '9'))
#define asc_oct_check(ch) (((ch) >= '0') && ((ch) <= '7'))
#define asc_hex_check(ch) (asc_dec_check(ch) || (((ch) >= 'A') && ((ch) <= 'F')) || (((ch) >= 'a') && ((ch) <= 'f')))

// loop
#define loop(x) for (int index = 0; index < (x); index++)
#define each(array) for (int index = 0; index < sizeof(array) / sizeof(array[0]); index++)
#define loop_n(x) for (int index = 0; index < (x); index++)

	/*****************************************************************************
* @brief   UINT8 UINT16 UINT32 BITS
*****************************************************************************/
	typedef union
	{
		uint8_t Val;
		uint8_t uVal;
		int8_t iVal;
		uint8_t uval;
		int8_t ival;
		struct
		{
			uint8_t b0 : 1;
			uint8_t b1 : 1;
			uint8_t b2 : 1;
			uint8_t b3 : 1;
			uint8_t b4 : 1;
			uint8_t b5 : 1;
			uint8_t b6 : 1;
			uint8_t b7 : 1;
		} bits;
	} UINT8_VAL, UINT8_BITS;

	typedef union
	{
		uint16_t Val;
		uint16_t uVal;
		int16_t iVal;
		uint16_t uval;
		int16_t ival;
		uint8_t v[2];
		struct
		{
			uint8_t LB;
			uint8_t HB;
		} byte;

		struct
		{
			uint16_t b0 : 1;
			uint16_t b1 : 1;
			uint16_t b2 : 1;
			uint16_t b3 : 1;
			uint16_t b4 : 1;
			uint16_t b5 : 1;
			uint16_t b6 : 1;
			uint16_t b7 : 1;
			uint16_t b8 : 1;
			uint16_t b9 : 1;
			uint16_t b10 : 1;
			uint16_t b11 : 1;
			uint16_t b12 : 1;
			uint16_t b13 : 1;
			uint16_t b14 : 1;
			uint16_t b15 : 1;
		} bits;
	} UINT16_VAL, UINT16_BITS;

	typedef union
	{
		uint32_t Val;
		uint32_t uVal;
		int32_t iVal;
		uint32_t uval;
		int32_t ival;
		uint16_t w[2];
		uint8_t v[4];

		struct
		{
			uint8_t LB;
			uint8_t HB;
			uint8_t UB;
			uint8_t MB;
		} byte;

		struct
		{
			uint16_t LW;
			uint16_t HW;
		} word;

		struct
		{
			uint32_t b0 : 1;
			uint32_t b1 : 1;
			uint32_t b2 : 1;
			uint32_t b3 : 1;
			uint32_t b4 : 1;
			uint32_t b5 : 1;
			uint32_t b6 : 1;
			uint32_t b7 : 1;
			uint32_t b8 : 1;
			uint32_t b9 : 1;
			uint32_t b10 : 1;
			uint32_t b11 : 1;
			uint32_t b12 : 1;
			uint32_t b13 : 1;
			uint32_t b14 : 1;
			uint32_t b15 : 1;
			uint32_t b16 : 1;
			uint32_t b17 : 1;
			uint32_t b18 : 1;
			uint32_t b19 : 1;
			uint32_t b20 : 1;
			uint32_t b21 : 1;
			uint32_t b22 : 1;
			uint32_t b23 : 1;
			uint32_t b24 : 1;
			uint32_t b25 : 1;
			uint32_t b26 : 1;
			uint32_t b27 : 1;
			uint32_t b28 : 1;
			uint32_t b29 : 1;
			uint32_t b30 : 1;
			uint32_t b31 : 1;
		} bits;
	} UINT32_VAL, UINT32_BITS;

	/*BYTE WORD DWORD BITS */
	typedef union
	{
		uint8_t Val;
		uint8_t uVal;
		int8_t iVal;
		uint8_t uval;
		int8_t ival;
		struct
		{
			uint8_t b0 : 1;
			uint8_t b1 : 1;
			uint8_t b2 : 1;
			uint8_t b3 : 1;
			uint8_t b4 : 1;
			uint8_t b5 : 1;
			uint8_t b6 : 1;
			uint8_t b7 : 1;
		} bits;
	} BYTE_VAL, BYTE_BITS, byte_tt;

	typedef union
	{
		uint16_t Val;
		uint16_t uVal;
		int16_t iVal;
		uint16_t uval;
		int16_t ival;
		uint8_t v[2];
		struct
		{
			byte_tt LB;
			byte_tt HB;
		} byte;

		struct
		{
			uint16_t b0 : 1;
			uint16_t b1 : 1;
			uint16_t b2 : 1;
			uint16_t b3 : 1;
			uint16_t b4 : 1;
			uint16_t b5 : 1;
			uint16_t b6 : 1;
			uint16_t b7 : 1;
			uint16_t b8 : 1;
			uint16_t b9 : 1;
			uint16_t b10 : 1;
			uint16_t b11 : 1;
			uint16_t b12 : 1;
			uint16_t b13 : 1;
			uint16_t b14 : 1;
			uint16_t b15 : 1;
		} bits;
	} WORD_VAL, WORD_BITS, word_tt;

	typedef union
	{
		uint32_t Val;
		uint32_t uVal;
		int32_t iVal;
		uint32_t uval;
		int32_t ival;
		uint16_t w[2];
		uint8_t v[4];

		struct
		{
			byte_tt LB;
			byte_tt HB;
			byte_tt UB;
			byte_tt MB;
		} byte;

		struct
		{
			word_tt LW;
			word_tt HW;
		} word;

		struct
		{
			uint32_t b0 : 1;
			uint32_t b1 : 1;
			uint32_t b2 : 1;
			uint32_t b3 : 1;
			uint32_t b4 : 1;
			uint32_t b5 : 1;
			uint32_t b6 : 1;
			uint32_t b7 : 1;
			uint32_t b8 : 1;
			uint32_t b9 : 1;
			uint32_t b10 : 1;
			uint32_t b11 : 1;
			uint32_t b12 : 1;
			uint32_t b13 : 1;
			uint32_t b14 : 1;
			uint32_t b15 : 1;
			uint32_t b16 : 1;
			uint32_t b17 : 1;
			uint32_t b18 : 1;
			uint32_t b19 : 1;
			uint32_t b20 : 1;
			uint32_t b21 : 1;
			uint32_t b22 : 1;
			uint32_t b23 : 1;
			uint32_t b24 : 1;
			uint32_t b25 : 1;
			uint32_t b26 : 1;
			uint32_t b27 : 1;
			uint32_t b28 : 1;
			uint32_t b29 : 1;
			uint32_t b30 : 1;
			uint32_t b31 : 1;
		} bits;
	} DWORD_VAL, DWORD_BITS, dword_tt;

	typedef union
	{
		uint64_t Val;
		uint64_t uVal;
		int64_t iVal;
		uint64_t uval;
		int64_t ival;
		uint32_t dw[2];
		uint16_t w[4];
		uint8_t v[8];

		struct
		{
			word_tt LW;
			word_tt HW;
			word_tt UW;
			word_tt MW;
		} word;

		struct
		{
			dword_tt LDW;
			dword_tt HDW;
		} dword;
	} QWORD_VAL, QWORD_BITS, qword_tt;

/*****************************************************************************
* @brief   dat tt
*****************************************************************************/
#define uun_tt_gen
	typedef union
	{
		uu8 uval;
		ii8 ival;
		struct
		{
			uu8 b0 : 1;
			uu8 b1 : 1;
			uu8 b2 : 1;
			uu8 b3 : 1;
			uu8 b4 : 1;
			uu8 b5 : 1;
			uu8 b6 : 1;
			uu8 b7 : 1;
		} bits;
	} u08_tt;

	typedef union
	{
		uu16 uval;
		ii16 ival;
		u08_tt v[2];
		struct
		{
			u08_tt lb;
			u08_tt hb;
		} byte;
	} u16_tt;

	typedef union
	{
		uu32 uval;
		ii32 ival;
		u08_tt v[4];
		u16_tt w[2];
		struct
		{
			u16_tt lw;
			u16_tt hw;
		} word;
		struct
		{
			u08_tt lb;
			u08_tt hb;
			u08_tt ub;
			u08_tt mb;
		} byte;
	} u32_tt;

	typedef union
	{
		uu64 uval;
		ii64 ival;
		u32_tt dw[2];
		u16_tt w[4];
		u08_tt v[8];
		struct
		{
			u16_tt lw;
			u16_tt hw;
			u16_tt uw;
			u16_tt mw;
		} word;
		struct
		{
			u32_tt ldw;
			u32_tt hdw;
		} dword;
	} u64_tt;

/*****************************************************************************
* @brief   led
*****************************************************************************/
#define LED_SEG_STRUCT
	typedef union
	{
		uint8_t val;
		struct
		{
			uint8_t A : 1;
			uint8_t B : 1;
			uint8_t C : 1;
			uint8_t D : 1;
			uint8_t E : 1;
			uint8_t F : 1;
			uint8_t G : 1;
			uint8_t DP : 1;
		} seg;
	} led_seg_type;

#ifdef __cplusplus
}
#endif

#endif /* MYTYPE_H */
